$(document).ready(function() {
	/*查询支出*/
	selectExClss();
});

/*查询支出费用  已经完成*/
function selectExClss() {
	$.ajax({
		url: bulicUrl + "class/selectclass.php",
		data: {

		},
		type: "POST",
		dataType: "text",
		success: function(data) {
			var datah = JSON.parse(data);
			var html = `
		        <thead>
        			<tr>
		                <td>序号</td>
		                <td>姓名</td>
		                <td>密码</td>
		                <td>电话</td>
		                <td>申请人姓名</td>
		                <td>申请时间</td>
		                <td>结束时间</td>
		                <td>备注</td>
		                <td>操作</td>
	                </tr>
            	</thead>
            	<tbody>`;
			var agree = "";

			if(datah == null) {
				html += `</tbody>`;
				$("#classTable").html(html);
				return;
			} else {

			}
			var zong = 0;
			for(var i = 0; i < datah.length; i++) {
				var data = datah[i];
				html += `<tr>
				
				            		<td>` + data['id'] + `</td>
				            		<td>` + data['name'] + `</td>
				            		<td>` + data['password'] + `</td>
				            		<td >` + data['phone'] + `</td>
				            		<td >` + data['peopleName'] + `</td>
				            		<td >` + data['time'] + `</td>
				            		<td>` + data['andtime'] + `</td>
				            		<td>` + data['reason'] + `</td>

				            		<td >
				            		<button  id="` + data['id'] + `"  class='updated btn btn-info btn-mini' data-toggle='modal'   href='#mymodal-link' onclick='ratify(this);'>编辑</button>`;
				/*最高管理员可以删除*/
				
				if(window.sessionStorage["scale"] == "1") {
					html += `      <button  id="` + data['id'] + `"  class='updated btn btn-info btn-mini' href='#mymodal-link' onclick='delectR(this);'>删除</button>`;
				}
				else{
					
				}
				html += `</td>
		            		</tr>`;

			}
			html += `</tbody>`;
			$("#classTable").html(html);

		}
	})
}
/*添加支出按钮  已经完成*/
function addClass() {
	$("input[name='peopleName']").attr("value", window.sessionStorage["userName"]);
	$("input[name='addorupdate']").attr("value", 0); //0添加/1修改
	nullLay();
}
/*添加支出  已经完成*/
function addclassInsert() {
	if($("input[name='addorupdate']").val() == 0) {
		//添加
		$("input[name='peopleName']").attr("value", window.sessionStorage["userName"]);
		$.ajax({
			url: bulicUrl + "class/addClass.php",
			data: {
				name: $("input[name='name']").val(),
				phone: $("input[name='phone']").val(),
				password: $("input[name='password']").val(),
				peopleName: $("input[name='peopleName']").val(),
				time: $("input[name='time']").val(),
				andtime: $("input[name='andtime']").val(),
				reason: $("#reason").val()
			},
			type: "POST",
			dataType: "text",
			success: function(data) {
				if(data == 1) {
					nullLay();
					alert("添加成功");
					/*查询支出*/
					selectExClss();

				} else {
					alert("添加失败")
				}
			}
		})
	} else {
		/*修改*/
		/*id:$("input[name='id']").val()*/
		$.ajax({
			url: bulicUrl + "class/updateClass.php",
			data: {
				id: $("input[name='id']").val(),
				name: $("input[name='name']").val(),
				phone: $("input[name='phone']").val(),
				password: $("input[name='password']").val(),
				peopleName: $("input[name='peopleName']").val(),
				time: $("input[name='time']").val(),
				andtime: $("input[name='andtime']").val(),
				reason: $("#reason").val()
			},
			type: "POST",
			dataType: "text",
			success: function(data) {
				if(data == 1) {
					
					alert("修改成功");
					/*查询支出*/
					selectExClss();
					nullLay();
				} else {
					alert("修改失败")
				}
			}
		})

	}

}

/*清空 已经完成*/
function nullLay() {
	setVal("name", "");
	setVal("phone", "");
	setVal("password", "");
	$("#reason").val("");
	setVal("id", "0");
	setVal("time",  getTime());
	setVal("andtime",  getTime());
	/*如果是最高管理员是可以更改结束状态的*/
	ratiAgreeAnd();
}
/*如果是最高管理员是可以更改结束状态的 已经完成*/
function ratiAgreeAnd() {
	if(window.sessionStorage["scale"] == "1") {
		$('#andtime').removeAttr("disabled");
	} else {
		$('#andtime').attr("disabled");
	}
}
/*编辑时候去查询一个单独的报销支出*/
function ratify(event) {
	/*如果是最高管理员是可以更改报销状态的*/
	ratiAgreeAnd();
	/*设置编辑*/
	$("input[name='addorupdate']").attr("value", 1);
	var idd = $(event).parent().siblings()[0].innerText;
	$("input[name='id']").attr("value", idd);
	var dta;
	$.ajax({
		url: bulicUrl + "class/selectclassOne.php",
		data: {
			id: idd,
		},
		type: "POST",
		dataType: "text",
		success: function(data) {
			dta = JSON.parse(data)[0]
			$("input[name='id']").val(dta.id)
			setVal("name", dta.name);
			setVal("password", dta.password);
			
			setVal("phone", dta.phone);
			setVal("peopleName", dta.peopleName);
			setVal("time", dta.time);
			setVal("andtime", dta.andtime);
			$("#reason").val(dta.reason);
		}
	})
}
/*{删除}  已经完成*/
function delectR(event) {
	if(confirm('确定删除此条信息?')) {
		$.ajax({
			url: bulicUrl + "class/delclassOne.php",
			data: {
				id: $(event).attr("id")
			},
			type: "POST",
			dataType: "text",
			success: function(data) {
				if(data == 1) {
					alert("删除成功");
					/*查询支出*/
					selectExClss();
				} else {
					alert("删除失败")
				}
			}
		})
	}
}